#!/usr/bin/python2 -S
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import site
import traceback
import codecs
import os
import sqlite3
#import pprint
import subprocess


import  jqdatasdk as jq

import data_struct 
import jq_acc

import magic
import rotate
import kline_downloader
import broker_pb_policy
import xrxd_research
import fundamental_research
import rk

MY_NAME='mf'

# 工具入口

def print_usage( argv0 = MY_NAME ):
    print "Usage:"



def make_sure_working_dir():
    r = subprocess.call([ "mkdir"
            , "-p"
            , data_struct.WORKING_DIR 
            ])

    if 0 != r:
        raise Exception("Failed to make '%s'" % data_struct.WORKING_DIR );


def main(): 
    
    i = len(sys.argv)
    if ( i < 2 ):
        print_usage()
        return 1

    jq.auth(jq_acc.JQ_USERNAME ,jq_acc.JQ_PASSWORD )

    make_sure_working_dir()

    sub_command = sys.argv[1]

    if ('help' == sub_command ):
        print_usage()
        return 1
    elif ('fetch' == sub_command ):  # 下载神奇公式所需报表数据    fetch [startyear]
        return magic.handle_fetch ( sys.argv[2:], MY_NAME  )   
    elif ('nofetch' == sub_command ):  # 编制神奇公式的各期成份
        return magic.handle_nofetch ( sys.argv[2:], MY_NAME  )
    elif ('nofetch2' == sub_command ): # 编制神奇公式的各期成份(shao加强)    nofetch2 [startyear]
        return magic.handle_nofetch2 ( sys.argv[2:], MY_NAME  )
    elif ('backtest' == sub_command or 'bt' == sub_command):  # 回测神奇公式  bt [startyear]
        return magic.handle_backtest ( sys.argv[2:], MY_NAME  )
    elif ('list_index' == sub_command ):  # 列出指数的成份
        return magic.handle_list_index ( sys.argv[2:], MY_NAME )   
    elif ('exper' == sub_command ):
        return magic.handle_exper ( sys.argv[2:], MY_NAME )  
    elif ('fetch50' == sub_command ):  # 下载50指数的成份股的日线
        return rotate.handle_fetch50 ( sys.argv[2:], MY_NAME  )   
    elif ('fast_horse_50' == sub_command or 'fh50' == sub_command):  # 回测50指数成份骑快马策略
        return rotate.handle_fh50 ( sys.argv[2:], MY_NAME  )
    elif ('slow_horse_50' == sub_command or 'sh50' == sub_command):  # 回测50指数成份骑慢马策略  sh50 [startday] [endday] [window size]
        return rotate.handle_sh50 ( sys.argv[2:], MY_NAME  )
    elif ('hh_line' == sub_command or 'hh' == sub_command):  # 下载指定代码的半小时线  hh [startday def '2008-01-01'] [code def '000300.XSHG']
        return kline_downloader.handle_hh( sys.argv[2:], MY_NAME  )

    elif ('fetchpool' == sub_command  or 'fetchp' == sub_command ):  # 下载指定股票池的日线
        return rotate.handle_fetch_in_pool ( sys.argv[2:], MY_NAME  )   
    elif ('slow_horse_pool' == sub_command or 'shp' == sub_command):  # 在指定股票池中回测骑慢马策略  shp [startday] [endday] [window size]
        return rotate.handle_sh_in_pool ( sys.argv[2:], MY_NAME  )
    elif ( 'shp2' == sub_command):  # 在指定股票池中回测相对均线最糟策略  shp [startday] [endday] [ma size]
        return rotate.handle_shp2_in_pool ( sys.argv[2:], MY_NAME  )
 
    elif ('hh_all' == sub_command or 'hha' == sub_command):  # 下载所有代码的半小时线  hha [startday def '2008-01-01'] [endday(不包括) def today ]
        return kline_downloader.handle_hha( sys.argv[2:], MY_NAME  )
    elif ('fetch_brk' == sub_command ):  # 下载券商PB策略所需报表数据    fetch_brk [startyear] [endyear] 
        return broker_pb_policy.handle_fetch_brk( sys.argv[2:], MY_NAME  )
    elif ('bt_brk' == sub_command ):  # 回测下载券商PB策略(轮换)          bt_brk [startday] [endday] [threshold] 
        return broker_pb_policy.handle_bt_brk( sys.argv[2:], MY_NAME  )
    elif ('bt_one_brk' == sub_command ):  # 回测下载券商PB策略(单个股票)  bt_one_brk [code]  [startday] [endday] [threshold] 
        return broker_pb_policy.handle_bt_one_brk( sys.argv[2:], MY_NAME  )

    elif ('fetch_xrxd' == sub_command ):  # 下载已经实施的分红数据    fetch_xrxd [startyear] [endyear]
        return xrxd_research. handle_fetch_xrxd( sys.argv[2:], MY_NAME )
    elif ('fetch_xrxd_b' == sub_command ):  # 下载董事会预告阶段的分红数据    fetch_xrxd_b [startyear] [endyear]
        return xrxd_research. handle_fetch_xrxd_b( sys.argv[2:], MY_NAME )
    #elif ('sum_xrxd' == sub_command ):  # 汇总除权除息公告日/登记日的行情    sum_xrxd [startyear] [endyear]
    #    return xrxd_research.handle_sum_xrxd( sys.argv[2:], MY_NAME )
    elif ('sum_xrxd2' == sub_command ):  # 汇总除权除息公告日/登记日开始一段期间的行情  sum_xrxd2 [startyear] [endyear]
        return xrxd_research.handle_sum_xrxd2( sys.argv[2:], MY_NAME )
    
    elif ('fetch_forcast' == sub_command ):  # 下载预告数据    fetch_forcast [startyear] [endyear]
        return xrxd_research. handle_fetch_forcast( sys.argv[2:], MY_NAME )
    elif ('sum_forcast' == sub_command ):  # 汇总预告后的行情    sum_forcast [startyear] [endyear]
        return xrxd_research.handle_sum_forcast( sys.argv[2:], MY_NAME )
  
    elif ('fetch_funda' == sub_command ):  # 下载基本面数据   fetch_funda [startyear] [endyear]
        return fundamental_research.handle_fetch_funda( sys.argv[2:], MY_NAME )
    elif ('sum_funda' == sub_command ):  # 汇总基本面数据   sum_funda [startyear] [endyear]
        return fundamental_research.handle_sum_funda( sys.argv[2:], MY_NAME )
 
    elif ('daily_line' == sub_command or 'dl' == sub_command):  # 下载指定代码的日线 dl [startday def '2019-01-01'] [code def '000300.XSHG']
        return kline_downloader.handle_dl( sys.argv[2:], MY_NAME  )
    elif ('rsi_kdj' == sub_command or 'rk' == sub_command):  # 对日线进行rsi_kdj分析 rk [startday def '2019-01-01'] 
        return rk.handle_rk( sys.argv[2:], MY_NAME  )
    
    else:
        print "\n无效的‘子命令’ -- %s\n" % ( sub_command, )
        print_usage()
        return 1
  
    return 0

if __name__ == "__main__":
    r = main()
    sys.exit(r)


